package com.example.allshengmedical.system.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.allsheng.widget.AllShengDialog;
import com.allsheng.widget.AllShengDialog.ClickListenerInterface;
import com.example.allshengmedical.R;
import com.example.allshengmedical.base.BaseFragment;
import com.example.allshengmedical.system.activity.FragmentTemp1.CallBackValue;
import com.example.allshengmedical.system.activity.FragmentTemp1.Timetask;
import com.example.allshengmedical.util.ByAlert;
import com.itboye.allshengmedical.sci.DefaultSerialControl;
import com.itboye.allshengmedical.sci.ISerialControl;
import com.itboye.allshengmedical.sci.cmd.req.CmdType;
import com.itboye.allshengmedical.sci.packet.resp.BaseRespPacket;
import com.itboye.allshengmedical.sci.packet.resp.GetTempRespPacket;

public class FragmentTemp2 extends BaseFragment implements OnClickListener {
	TextView start_5, now_5_temp;
	TextView start_55, now_55_temp;
	TextView start_72, now_temp_72;
	TextView start_95, now_temp_95;
	TextView ok;
	EditText input_5_temp, input_55_temp, input_temp_72, input_temp_95;
	ISerialControl controlInstance;
	boolean isFirst = true;
	boolean isSecond = true;
	boolean isThree = true;
	boolean isFourth = true;
	CallBackValue callBackValue;
	Timer timer;
	boolean isVerTemp = false;

	@Override
	public int getlayout() {
		// TODO Auto-generated method stub
		return R.layout.fragment_temp2;
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		controlInstance = DefaultSerialControl.getInstance();
		controlInstance.init();
		controlInstance.start();
		controlInstance.setHandler(mHandler);
	}

	@Override
	protected void dataView() {
		// TODO Auto-generated method stub
		NumberFormat format = new NumberFormat();
		format.Format(input_55_temp);
		format.Format(input_5_temp);
		format.Format(input_temp_72);
		format.Format(input_temp_95);
		if (isFirst && isFourth && isThree && isSecond) {
			callBackValue.SendMessageValue("isChange");
		}
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		// 当前fragment从activity重写了回调接口 得到接口的实例化对象
		callBackValue = (CallBackValue) getActivity();
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case ISerialControl.FindPacketMessage:
				// 完成主界面更新,拿到数据
				update(msg.getData());
				break;
			case ISerialControl.SCTimeoutMessage:
				String cmdType = msg.getData().getString("cmdType");
				if (cmdType.length() < 1)
					return;
				// 根据type 转换packet 为你想要的
				CmdType type = CmdType.getCmdType(cmdType.substring(1));
				// 发送指令后这里会收到数据
				Log.d("hu timeout", "type=" + cmdType + type.toDesc());
				switch (type) {
				case VerifyTemp: {
					final AllShengDialog allShengDialog = new AllShengDialog(getActivity(), "", "超时", "ok");
					allShengDialog.show();
					allShengDialog.setClicklistener(new ClickListenerInterface() {

						@Override
						public void OK() {
							// TODO Auto-generated method stub
							allShengDialog.dismiss();
						}

						@Override
						public void Cancle() {
							// TODO Auto-generated method stub
							allShengDialog.dismiss();

						}
					});
				}
					break;
				case GetTemp:
					final AllShengDialog allShengDialog = new AllShengDialog(getActivity(), "", "超时", "ok");
					allShengDialog.show();
					allShengDialog.setClicklistener(new ClickListenerInterface() {

						@Override
						public void OK() {
							// TODO Auto-generated method stub
							allShengDialog.dismiss();
						}

						@Override
						public void Cancle() {
							// TODO Auto-generated method stub
							allShengDialog.dismiss();

						}
					});
					break;
				}
			}
		}

		private void update(Bundle bundle) {
			// TODO Auto-generated method stub
			String cmdType = bundle.getString("cmdType");
			BaseRespPacket packet = (BaseRespPacket) bundle.getSerializable("packet");
			if (cmdType.length() < 1)
				return;
			// 根据type 转换packet 为你想要的
			CmdType type = CmdType.getCmdType(cmdType.substring(1));
			// 发送指令后这里会收到数据
			Log.d("read_data", "接受到的响应数据包是" + cmdType + type.toDesc());
			switch (type) {
			case GetTemp:
				GetTempRespPacket tempResp = new GetTempRespPacket(packet);
				if (!isFirst) {
					now_5_temp.setText(tempResp.ModeTwo());
				} else if (!isSecond) {
					now_55_temp.setText(tempResp.ModeTwo());
				} else if (!isThree) {
					now_temp_72.setText(tempResp.ModeTwo());
				} else if (!isFourth) {
					now_temp_95.setText(tempResp.ModeTwo());
				}

				break;
			case VerifyTemp:
				final AllShengDialog allShengDialog = new AllShengDialog(getActivity(), "", "校温成功", "ok");
				allShengDialog.show();
				allShengDialog.setClicklistener(new ClickListenerInterface() {

					@Override
					public void OK() {
						// TODO Auto-generated method stub
						allShengDialog.dismiss();
					}

					@Override
					public void Cancle() {
						// TODO Auto-generated method stub
						allShengDialog.dismiss();

					}
				});
				break;
			}
		}
	};

	public String isEmpty() {
		StringBuilder builder = new StringBuilder();
		if (!"".equals(input_5_temp.getText().toString()) && Double.parseDouble(input_5_temp.getText().toString()) >= 0
				&& Double.parseDouble(input_5_temp.getText().toString()) <= 110) {

		} else {
			builder.append("5度参数非法;");
		}
		if (!"".equals(input_55_temp.getText().toString()) && Double.parseDouble(input_55_temp.getText().toString()) >= 0
				&& Double.parseDouble(input_55_temp.getText().toString()) <= 110) {

		} else {
			builder.append("55度参数非法;");
		}
		if (!"".equals(input_temp_72.getText().toString()) && Double.parseDouble(input_temp_72.getText().toString()) >= 0
				&& Double.parseDouble(input_temp_72.getText().toString()) <= 110) {

		} else {
			builder.append("72度参数非法;");
		}
		if (!"".equals(input_temp_95.getText().toString()) && Double.parseDouble(input_temp_95.getText().toString()) >= 0
				&& Double.parseDouble(input_temp_95.getText().toString()) <=110) {

		} else {
			builder.append("95度参数非法;");
		}

		return builder.toString();

	}

	class Timetask extends TimerTask {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			Message message = new Message();
			message.what = 1;
			handler.sendMessage(message);
		}

	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 1) {
				controlInstance.addCommand(CmdType.GetTemp, null, 0);
			}
		};
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.start_5:
			if (isFirst) {
				start_5.setText("结束");
				start_55.setEnabled(false);
				start_72.setEnabled(false);
				start_95.setEnabled(false);
				callBackValue.SendMessageValue("noChange");
				controlInstance.addCommand(CmdType.SetTemp, "CtrlTemp2", 50);
				timer = new Timer();
				timer.schedule(new Timetask(), 1500, 1500);

			} else {
				start_5.setText("开始");
				start_55.setEnabled(true);
				start_72.setEnabled(true);
				start_95.setEnabled(true);
				callBackValue.SendMessageValue("isChange");
				controlInstance.addCommand(CmdType.GetTemp, "", 0);
				timer.cancel();
			}
			isFirst = !isFirst;

			break;
		case R.id.start_55:
			if (isSecond) {
				start_55.setText("结束");
				start_5.setEnabled(false);
				start_72.setEnabled(false);
				start_95.setEnabled(false);
				callBackValue.SendMessageValue("noChange");
				controlInstance.addCommand(CmdType.SetTemp, "CtrlTemp2", 550);
				timer = new Timer();
				timer.schedule(new Timetask(), 1500, 1500);
			} else {
				start_55.setText("开始");
				start_5.setEnabled(true);
				start_72.setEnabled(true);
				start_95.setEnabled(true);
				callBackValue.SendMessageValue("isChange");
				controlInstance.addCommand(CmdType.GetTemp, "", 0);
				timer.cancel();
			}
			isSecond = !isSecond;

			break;
		case R.id.start_72:
			if (isThree) {
				start_72.setText("结束");
				start_5.setEnabled(false);
				start_55.setEnabled(false);
				start_95.setEnabled(false);
				callBackValue.SendMessageValue("noChange");
				controlInstance.addCommand(CmdType.SetTemp, "CtrlTemp2", 720);
				timer = new Timer();
				timer.schedule(new Timetask(), 1500, 1500);
			} else {
				start_72.setText("开始");
				start_5.setEnabled(true);
				start_55.setEnabled(true);
				start_95.setEnabled(true);
				callBackValue.SendMessageValue("isChange");
				controlInstance.addCommand(CmdType.GetTemp, "", 0);
				timer.cancel();
			}
			isThree = !isThree;
			break;
		case R.id.start_95:
			if (isFourth) {
				start_95.setText("结束");
				start_5.setEnabled(false);
				start_72.setEnabled(false);
				start_55.setEnabled(false);
				callBackValue.SendMessageValue("noChange");
				controlInstance.addCommand(CmdType.SetTemp, "CtrlTemp2", 950);
				timer = new Timer();
				timer.schedule(new Timetask(), 1500, 1500);
			} else {
				start_95.setText("开始");
				start_5.setEnabled(true);
				start_72.setEnabled(true);
				start_55.setEnabled(true);
				callBackValue.SendMessageValue("isChange");
				controlInstance.addCommand(CmdType.GetTemp, "", 0);
				timer.cancel();
			}
			isFourth = !isFourth;
			break;
		case R.id.ok:
			if (isFirst && isFourth && isThree && isSecond) {
				callBackValue.SendMessageValue("isChange");
			}
			String temp5 = input_5_temp.getText().toString();
			String temp55 = input_55_temp.getText().toString();
			String temp72 = input_temp_72.getText().toString();
			String temp95 = input_temp_95.getText().toString();

			if ("".equals(isEmpty())) {
				isVerTemp = true;
				NumberFormat format = new NumberFormat();
				controlInstance.addCommand(CmdType.VerifyTemp, "VerifyTemp2", format.translateNum(temp5),
						format.translateNum(temp55), format.translateNum(temp72), format.translateNum(temp95));
			} else {
				ByAlert.alert(isEmpty());
			}

			break;
		default:
			break;
		}
	}
}